package com.cho3.module;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;

public class ImagePickerModule extends ReactContextBaseJavaModule implements ActivityEventListener {
    static final int REQUEST_LAUNCH_IMAGE_LIBRARY = 13002;
    private ReactApplicationContext mReactContext;
    private Callback mCalLBack;
    private WritableMap mResponse;

    public ImagePickerModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mReactContext = reactContext;
        reactContext.addActivityEventListener(this);
    }

    @Override
    public String getName() {
        return "ImagePicker";
    }

    @ReactMethod
    public void launchImagePicker(final ReadableMap options, final Callback callback) {
        mCalLBack = callback;
        mResponse = Arguments.createMap();
        Intent libraryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//        libraryIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
//                "image/*");
        //打开图库
        try {
            int requestCode = REQUEST_LAUNCH_IMAGE_LIBRARY;
            Activity currentActivity = getCurrentActivity();
            currentActivity.startActivityForResult(libraryIntent, requestCode);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            if (mCalLBack != null) {
                mResponse.putString("error", "Cannot launch photolibrary");
                mCalLBack.invoke(mResponse);
                mCalLBack = null;
            }
        }
    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        mResponse = Arguments.createMap();
        //用户取消
        if (resultCode != Activity.RESULT_OK) {
            mResponse.putBoolean("didCancel", true);
            mCalLBack.invoke(mResponse);
            return;
        }
        Uri uri = data.getData();
        String realPath = getRealPathFromURI(uri);
        if (!TextUtils.isEmpty(realPath)) {
            //解码图片
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(realPath, options);
            //回调函数
            mResponse.putString("uri", uri.toString());
            mResponse.putString("path", realPath);
            mCalLBack.invoke(mResponse);
            mCalLBack = null;
        }
    }

    private String getRealPathFromURI(Uri uri) {
        String result;
        String[] projection = {MediaStore.Images.Media.DATA};
        if (uri == null || TextUtils.isEmpty(uri.toString())) {
            return "";
        }
        Cursor cursor = mReactContext.getContentResolver().query(uri, projection, null, null, null);
        if (cursor == null) {
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }

    @Override
    public void onNewIntent(Intent intent) {

    }
}
